package com.lanp.native_test.ExecutorsRun.CustomTheadExecutor;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

public class ArrayBlockingQueue_Test {

    public static final int PROCESSORS = Runtime.getRuntime().availableProcessors();
    /*
    数组、先进先出
    最大任务容量是 最大线程数 + 队列最大长度
    不超过 阻塞队列数量，执行核心线程数量
    超过 阻塞队列数量，执行最大线程数量
     */
    static BlockingQueue blockingQueue = new ArrayBlockingQueue<>(15);


    private static final ThreadPoolExecutor TEST_EXECUTORS =
            new ThreadPoolExecutor(1, 4, 10, TimeUnit.MINUTES
                    , blockingQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws InterruptedException {

        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(1111);
            }
        };
        TEST_EXECUTORS.execute(runnable2);
        for (int i = 0; i < 20; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    Date nowDate = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println(sdf.format(nowDate) + "   " + fi + "-索引-newFixedThreadPool:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            System.out.println("准备添加到队列" + blockingQueue.offer(runnable,3050,TimeUnit.MILLISECONDS));
            //TEST_EXECUTORS.execute(runnable);
        }

    }
}
